package org.suyafei.cotroller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.suyafei.constant.RedisKey;
import org.suyafei.cotroller.request.ConfirmOrderRequest;
import org.suyafei.cotroller.request.ProductOrderPageRequest;
import org.suyafei.enums.BizCodeEnum;
import org.suyafei.enums.ClientTypeEnum;
import org.suyafei.enums.ProductOrderPayTypeEnum;
import org.suyafei.interceptor.LoginInterceptor;
import org.suyafei.service.ProductOrderService;
import org.suyafei.utils.CommonUtil;
import org.suyafei.utils.JsonData;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 宿某人
 * @Date: 2022/02/06/3:52 下午
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("api/order/v1")
public class ProductOrderController {

    @Autowired
    private ProductOrderService productOrderService;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @GetMapping("token")
    public JsonData getOrderToken() {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        String token = CommonUtil.getStringNumRandom(32);

        String key = String.format(RedisKey.PRODUCT_ORDER_TOKEN_KEY, accountNo, token);
        //令牌有效时间30分钟
        redisTemplate.opsForValue().set(key,String.valueOf( Thread.currentThread().getId()), 30, TimeUnit.MINUTES);

        return JsonData.buildSuccess(token);
    }

    @GetMapping("page")
    public JsonData page(@RequestBody ProductOrderPageRequest request ) {

        Map<String, Object> pageMap = productOrderService.page(request);
        return JsonData.buildSuccess(pageMap);
    }
    @GetMapping("query_state")
    public JsonData qureyState(@RequestParam(value = "out_trade_no")String outTradeNo){
        String state = productOrderService.queryProductOrderState(outTradeNo);
        return StringUtils.isBlank(state) ? JsonData.buildSuccess(BizCodeEnum.ORDER_CONFIRM_NOT_EXIST) : JsonData.buildSuccess(state);

    }

    @PostMapping("confirm")
    public void confirm(@RequestBody ConfirmOrderRequest orderRequest, HttpServletResponse response) {
        JsonData jsonData = productOrderService.confirmOrder(orderRequest);
        if (jsonData.getCode()== 0) {
            //端口类型
            String payType = orderRequest.getPayType();
            //支付类型
            String clientType = orderRequest.getClientType();

            //如果支付宝支付，跳转网页，sdk除外
            if (ProductOrderPayTypeEnum.ALI_PAY.name().equalsIgnoreCase(payType)) {
                if (ClientTypeEnum.PC.name().equalsIgnoreCase(clientType)) {
                    CommonUtil.sendHtmlMessage(response, jsonData);
                } else if (ClientTypeEnum.APP.name().equalsIgnoreCase(clientType)) {

                } else if (ClientTypeEnum.H5.name().equalsIgnoreCase(clientType)) {

                }
            } else if (ProductOrderPayTypeEnum.WECHAT_PAY.name().equalsIgnoreCase(payType)) {
                if (ClientTypeEnum.PC.name().equalsIgnoreCase(clientType)) {
//                    微信支付
                    CommonUtil.sendJsonMessage(response, jsonData); 
                } else if (ClientTypeEnum.APP.name().equalsIgnoreCase(clientType)) {

                } else if (ClientTypeEnum.H5.name().equalsIgnoreCase(clientType)) {

                }
            }
        }else {
            log.error("创建订单失败{}", jsonData.toString());
            CommonUtil.sendJsonMessage(response, jsonData);
        }
    }
}
